package com.stepforward.me;



import java.util.ArrayList;





import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class MyFavoriteHelper extends SQLiteOpenHelper{
	
	private static final int DATABASE_VERSION = 2;
	private static final String dbName="d4";
	private static final String MYFAVORITES_TABLE_NAME = "mss";
	private static final String KEY_NAME = "name";
	private static final String KEY_DEPARTURE = "departure";
	private static final String KEY_ARRIVAL ="arrival";
	private static final String KEY_ID="fid";
	private static final String MYFAVORITES_TABLE_CREATE =
	                "CREATE TABLE " + MYFAVORITES_TABLE_NAME + " (" +KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+KEY_NAME+" TEXT,"+KEY_DEPARTURE+" TEXT,"+KEY_ARRIVAL+" TEXT)";

	
	public MyFavoriteHelper(Context context) {
	    
		// TODO Auto-generated constructor stub
		super(context, dbName, null,DATABASE_VERSION);
		
		}
	@Override
	public void onCreate(SQLiteDatabase db) {
	
		// TODO Auto-generated method stub
		db.execSQL(MYFAVORITES_TABLE_CREATE);	
		ContentValues cv=new ContentValues();
		cv.put(KEY_ID, 1);
		cv.put(KEY_NAME, "home");
		cv.put(KEY_DEPARTURE, "Carnegie Mellon University");
		cv.put(KEY_ARRIVAL, "Murray at Forbes");
		db.insert(MYFAVORITES_TABLE_NAME, KEY_ID, cv);
		cv.put(KEY_ID, 2);
		cv.put(KEY_NAME, "school");
		cv.put(KEY_DEPARTURE, "Murray at Forbes");
		cv.put(KEY_ARRIVAL, "Carnegie Mellon University");
		db.insert(MYFAVORITES_TABLE_NAME, KEY_ID, cv);
		cv.put(KEY_ID, 3);			
		cv.put(KEY_NAME, "shopping");
		cv.put(KEY_DEPARTURE, "Murray at Forbes");
		cv.put(KEY_ARRIVAL, "Walnut");
		db.insert(MYFAVORITES_TABLE_NAME, KEY_ID, cv);
		
	}

	public Boolean insertFav(myFavorite myfav) {

		Boolean ok=false;
		SQLiteDatabase db=this.getWritableDatabase();
		String[] params=new String[]{String.valueOf(myfav.getColName().toString())};
		Cursor cur=db.rawQuery("SELECT * FROM "+MYFAVORITES_TABLE_NAME+" WHERE "+KEY_NAME+"=?",params);
		int x= cur.getCount();	
		if(x!=0)
			 return ok;
		ContentValues cv=new ContentValues();
		cv.put(KEY_DEPARTURE,myfav.getColDep());
		cv.put(KEY_ARRIVAL, myfav.getColArr());
		cv.put(KEY_NAME, myfav.getColName());
		db.insert(MYFAVORITES_TABLE_NAME,KEY_DEPARTURE, cv);
		db.close();
		ok=true;
		return ok;
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	 int getFavCount()
	 {
		SQLiteDatabase db=this.getWritableDatabase();
		Cursor cur= db.rawQuery("SELECT * FROM "+MYFAVORITES_TABLE_NAME, null);
		int x= cur.getCount();
		cur.close();
		return x;
	 }
	 
	 public ArrayList<String> getAllFavoritesName()
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 ArrayList<String>al=new ArrayList<String>();
		 //Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable, new String [] {});
		 Cursor cur= db.rawQuery("SELECT * FROM "+MYFAVORITES_TABLE_NAME,null);
         
         int size=cur.getCount();
         cur.moveToFirst();
         for (int i=0;i<size;i++)
         {
        	 String name=cur.getString(cur.getColumnIndex(KEY_NAME));
        	 cur.moveToNext();
        	
        	 al.add(name);
        	 }
         cur.close();
		 return al;
		 
	 }
	 public ArrayList<String> getAllFavoritesDepArr()
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 ArrayList<String>al=new ArrayList<String>();
		 //Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable, new String [] {});
		 Cursor cur= db.rawQuery("SELECT * FROM "+MYFAVORITES_TABLE_NAME,null);
         
         int size=cur.getCount();
         cur.moveToFirst();
         for (int i=0;i<size;i++)
         {
        	 
        	 String dep=cur.getString(cur.getColumnIndex(KEY_DEPARTURE));
        	 String arr=cur.getString(cur.getColumnIndex(KEY_ARRIVAL));
        	 cur.moveToNext();
        	 String name="From "+dep+" To "+arr;
        	
        	 al.add(name);
        	 }
         cur.close();
		 return al;
		 
	 }
	 
	 public String getArrByName(String name)
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 String[] params=new String[]{String.valueOf(name)};

		 Cursor c=db.rawQuery("SELECT "+KEY_ARRIVAL+" FROM "+MYFAVORITES_TABLE_NAME+" WHERE "+KEY_NAME+"=?",params);
		 c.moveToFirst();
		 int index= c.getColumnIndex(KEY_ARRIVAL);
		 return c.getString(index);
	 }
	 public String getDepByName(String name)
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 String[] params=new String[]{String.valueOf(name)};

		 Cursor c=db.rawQuery("SELECT "+KEY_DEPARTURE+" FROM "+MYFAVORITES_TABLE_NAME+" WHERE "+KEY_NAME+"=?",params);
		 c.moveToFirst();
		 int index= c.getColumnIndex(KEY_DEPARTURE);
		 return c.getString(index);
	 }

	 public int UpdateFav(myFavorite myfav)
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 ContentValues cv=new ContentValues();
		 cv.put(KEY_DEPARTURE, myfav.getColDep());
		 cv.put(KEY_ARRIVAL, myfav.getColArr());
		 return db.update(MYFAVORITES_TABLE_NAME, cv, KEY_NAME+"=?", new String []{String.valueOf(myfav.getColName())});
		 
	 }

	 
	 public void DeleteFav(myFavorite myfav)
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 db.delete(MYFAVORITES_TABLE_NAME,KEY_ID+"=?", new String [] {String.valueOf(myfav.getColID())});
		 db.close();
		 
		
		
	 }
	 public Boolean DeleteFav(String name)
	 {
		 Boolean ok=false;
		 SQLiteDatabase db=this.getWritableDatabase();
		 String[] params=new String[]{String.valueOf(name.toString())};
		 Cursor cur=db.rawQuery("SELECT * FROM "+MYFAVORITES_TABLE_NAME+" WHERE "+KEY_NAME+"=?",params);
		 int x= cur.getCount();	
		 if(x!=0)
		 { 
			 db.delete(MYFAVORITES_TABLE_NAME,KEY_NAME+"=?", new String [] {String.valueOf(name)});
			 ok=true;
			 db.close();
			 return ok;
			 
		 }
		 else
			 return ok;
		
	
	 }
}
